<div class="width65 floatLeft ads" id="ads_top_1"></div>
<?php
echo '<div class="width100 floatLeft" id="fb_result">';

if(!isset($_POST['next_page'])){
	$expand = new ExpandWord();
	$word_strip = strip_punctuation(urldecode($_GET['q']));	
	$expand_words = $expand->getWords(str_split_utf8($word_strip));	
	//先放 the original word
	$query = $word_strip.'|';
	foreach($expand_words as $keyword){
		$query .= $keyword.'|';	
	}
	$query = substr($query, 0, -1);
		
	$query = urlencode($query);
	$limit = 10;
	$offset = 0;	
	$url = "https://graph.facebook.com/search?q=$query&type=page&limit=$limit&offset=$offset";	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$body = curl_exec($ch);
	curl_close($ch);
	$json = json_decode($body);
}
else{
	$url = $_POST['next_page'];
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$body = curl_exec($ch);
	curl_close($ch);
	$json = json_decode($body);
}
	
	$next_page = '';
	$previous_page = '';
	if(isset($json->paging->next)){
		$next_page = $json->paging->next;
	}
	
	if(isset($json->paging->previous)){
		$previous_page = $json->paging->previous;
	}
	
	if(isset($json->data)){
		$results = $json->data;	
		$result_ids = '';
		$unique_ids = array();
		
		 foreach($results as $key=>$result){
		 	if(!isset($unique_ids[$result->id])){
		 		$result_ids .= $result->id . ',';
				$unique_ids[$result->id] = '';
		 	}
		 }
		 
		 if($result_ids != ''){
		    $result_ids = substr($result_ids, 0, -1);	 
		 	$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/?ids='.$result_ids);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$body = curl_exec($ch);
			curl_close($ch);
			$json = json_decode($body);
			echo '<div class="gradient width70 floatLeft">';
			foreach($json as $key=>$data){		
					echo '<h2><a href="'.$data->link.'" target="_blank">'.$data->name.'</a></h2>';	
					if(isset($data->description)){			
						echo '<ul style="cursor:pointer;" onclick="manageDescr(\'desc'.$key.'\')"><li>＋<div id="desc'.$key.'" style="display:none;">'.strip_tags(@$data->description).'</div></li></ul>';
					}
					else{
						echo '<ul><li>暫無內容描述</li></ul>';	
					}	
			}
			
			echo '<div class="alignCenter">';
			if($previous_page!=''){
				echo '<span class="green" onclick="gotoNextPage(\''.$previous_page.'\')" style="cursor:pointer">上一頁</span>&nbsp;&nbsp;|&nbsp;&nbsp;';
			}
			
			if($next_page!=''){
				echo '<span class="green" onclick="gotoNextPage(\''.$next_page.'\')" style="cursor:pointer">下一頁</span><span id="next_loading"></span>';
			}
			else{
				echo '<span>無更多結果</span>';
			}
			echo '</div>';
			
			echo '</div>';
			echo '<div class="width25 floatLeft ads" id="ads_right_bottom"></div>';
		 }		
	}
echo '<div class="width65 floatLeft ads" id="ads_bottom_1"></div>';
echo '</div>';
?>
